from datetime import datetime
from http.client import HTTPException
from typing import Optional

from sqlalchemy.orm import Session

from core.entity.do.models import CmsGaodeTerminal
from core.entity.vo.view_models import CmsGaodeTerminalVO


class CmsGaodeTerminalDao:
    """
    高德终端dao
    """

    @classmethod
    def get_cms_gaode_terminal_by_id(cls, db: Session, id: int):
        """
        根据id查询高德终端信息
        :param db: orm对象
        :param id: 主键ID
        :return: 高德终端信息对象
        """
        cms_gaode_terminal = db.query(CmsGaodeTerminal) \
            .filter(CmsGaodeTerminal.id == id) \
            .first()

        return cms_gaode_terminal

    @classmethod
    def get_cms_gaode_terminal_by_terminal_id(cls, db: Session, terminal_id: str, start_time: Optional[str] = None,
                                              end_time: Optional[str] = None) -> CmsGaodeTerminalVO:
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param terminal_id: 终端id
        :param start_time: 起始时间
        :param end_time: 结束时间
        :return: 高德终端信息对象
        """
        query = db.query(CmsGaodeTerminal).filter(CmsGaodeTerminal.device_id == terminal_id)

        if start_time:
            try:
                start_time_dt = datetime.fromisoformat(start_time)
                query = query.filter(CmsGaodeTerminal.timestamp >= start_time_dt)
            except ValueError as e:
                raise ValueError(f"Invalid start_time format: {e}")

        if end_time:
            try:
                end_time_dt = datetime.fromisoformat(end_time)
                query = query.filter(CmsGaodeTerminal.timestamp <= end_time_dt)
            except ValueError as e:
                raise ValueError(f"Invalid end_time format: {e}")

        cms_gaode_terminal = query.first()


        return CmsGaodeTerminalVO.from_orm(cms_gaode_terminal)
    @classmethod
    def get_cms_gaode_terminal_like_name(cls, db: Session,terminal_name: str):
        """
        获取所有的参数配置信息
        :param db: orm对象
        :param terminal_name: 终端名称
        :return: 高德终端信息对象集合
        """
        cms_gaode_terminal_list = db.query(CmsGaodeTerminal) \
            .filter(CmsGaodeTerminal.terminal_name.like(f'%{terminal_name}%') if terminal_name else True) \
            .all()

        return cms_gaode_terminal_list

    @classmethod
    def add_cms_gaode_terminal_dao(cls, db: Session, cms_gaode_terminal: CmsGaodeTerminalVO):
        """
        新增高德终端
        :param db: orm对象
        :param cms_gaode_terminal: 高德终端对象
        :return:
        """
        model_dict = CmsGaodeTerminal(**cms_gaode_terminal.dict())
        db.add(model_dict)
        db.flush()

        return model_dict

    @classmethod
    def edit_cms_gaode_terminal_dao(cls, db: Session, cms_gaode_terminal: dict):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param cms_gaode_terminal: 需要更新的参数配置字典
        :return:
        """
        db.query(CmsGaodeTerminal) \
            .filter(CmsGaodeTerminal.id == cms_gaode_terminal.get('id')) \
            .update(cms_gaode_terminal)

    @classmethod
    def delete_cms_gaode_terminal_dao(cls, db: Session, id: int):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param id: id
        :return:
        """
        db.query(CmsGaodeTerminal) \
            .filter(CmsGaodeTerminal.id == id) \
            .delete()
